package io.itit.ecp.admin.server.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.common.constants.EcpAdminConstants;
import io.itit.ecp.admin.common.enums.StatisticsModeEnums;
import io.itit.ecp.admin.common.utils.EcpAdminUtils;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.entity.extend.EnergyDataExtEntity;
import io.itit.ecp.admin.server.mapper.CarbonEmissionUnplanMapper;
import io.itit.ecp.admin.server.service.ICarbonEmissionPlanService;
import io.itit.ecp.admin.server.service.ICarbonEmissionUnplanService;
import io.itit.ecp.admin.server.service.ISceneEdificeService;
import io.itit.grass.common.server.helper.ConfigHelper;
import io.itit.grass.common.server.helper.ThreadPoolHelper;
import io.itit.grass.common.utils.Y666Utils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：碳排放未规划配置服务实现类
 * @Date：Create by 2024-01-19 14:15:35
 */
@Service
public class CarbonEmissionUnplanServiceImpl extends JoinServiceImpl<CarbonEmissionUnplanMapper, CarbonEmissionUnplanEntity> implements ICarbonEmissionUnplanService {

    @Resource
    private ISceneEdificeService sceneEdificeService;

//    @Resource
    private ICarbonEmissionPlanService carbonEmissionPlanService;

    @Override
//    @PostConstruct
    public void buildData(CarbonEmissionPlanEntity entity) {
//        ThreadPoolHelper.i().execute(() -> build(entity));
    }

    public void build(CarbonEmissionPlanEntity entity) {
        // 新增或修改已规划数据 ，先给同项目楼栋数据清理掉
        if (StringUtils.isNotEmpty(entity.getSceneProjectId()) && StringUtils.isNotEmpty(entity.getSceneEdificeId())) {
            this.remove(lambdaQuery().eq(CarbonEmissionUnplanEntity::getSceneProjectId, entity.getSceneProjectId())
                    .eq(CarbonEmissionUnplanEntity::getSceneEdificeId, entity.getSceneEdificeId()));
        }

        String startDt = ConfigHelper.i().getStr("ecp-admin.carbonEmissionUnplan.initDate");
        String endDt = Y666Utils.formatYyyyMmDd(new Date());

        // 先算日期
        List<String> dateList = EcpAdminUtils.dateStr(startDt, endDt, StatisticsModeEnums.day.mode);
        if (CollectionUtils.isEmpty(dateList)) {
            return;
        }

        List<SceneEdificeEntity> edificeList = sceneEdificeService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(entity.getSceneProjectId()), SceneEdificeEntity::getSceneProjectId, entity.getSceneProjectId())
                .eq(StringUtils.isNotEmpty(entity.getSceneEdificeId()), SceneEdificeEntity::getId, entity.getSceneEdificeId())
                .list();

        Set<String> allSet = new HashSet<>();
        for (SceneEdificeEntity edificeEntity : edificeList) {
            for (String date : dateList) {
                allSet.add(EcpAdminUtils.keyColon(edificeEntity.getId(), date));
            }
        }

        Set<String> existSet = new HashSet<>();
        List<CarbonEmissionPlanEntity> planList = carbonEmissionPlanService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(entity.getSceneProjectId()), CarbonEmissionPlanEntity::getSceneProjectId, entity.getSceneProjectId())
                .eq(StringUtils.isNotEmpty(entity.getSceneEdificeId()), CarbonEmissionPlanEntity::getId, entity.getSceneEdificeId())
                .list();
        for (CarbonEmissionPlanEntity planEntity : planList) {
            List<String> dateTmpList = EcpAdminUtils.dateStr(Y666Utils.formatYyyyMmDd(planEntity.getEffectiveTimeStart()), Y666Utils.formatYyyyMmDd(planEntity.getEffectiveTimeEnd()), StatisticsModeEnums.day.mode);
            for (String date : dateTmpList) {
                existSet.add(EcpAdminUtils.keyColon(planEntity.getSceneEdificeId(), date));
            }
        }

        allSet.removeAll(existSet);
        if (CollectionUtils.isEmpty(allSet)) {
            return;
        }

        Map<String, String> projectMap = edificeList.stream().collect(Collectors.toMap(SceneEdificeEntity::getId, SceneEdificeEntity::getSceneProjectId));

        List<CarbonEmissionUnplanEntity> unplanList = new ArrayList<>();
        for (String str : allSet) {
            CarbonEmissionUnplanEntity unplanEntity = new CarbonEmissionUnplanEntity();
            String[] keys = str.split(EcpAdminConstants.COLON);
            unplanEntity.setSceneProjectId(projectMap.get(unplanEntity.getSceneEdificeId()));
            unplanEntity.setSceneEdificeId(keys[0]);
            unplanEntity.setUnplanDate(Y666Utils.parseYyyyMmDd(keys[1]));
            unplanList.add(unplanEntity);
        }

        this.saveBatch(unplanList);
    }
}